package com.i2bt.adapter;

import com.i2bt.R;
import com.i2bt.app.friend.FriendDetailActivity;
import com.i2bt.bean.GetSharedBean;

import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class FriendDetailSharedListAdapter extends BaseAdapter {
	
	private FriendDetailActivity friendDetailActivity;

	private List<GetSharedBean> sharedFilesList;

	public FriendDetailSharedListAdapter(FriendDetailActivity 
			friendDetailActivity, List<GetSharedBean> sharedFilesList) {
		this.friendDetailActivity = friendDetailActivity;
		this.sharedFilesList = sharedFilesList;
		if (sharedFilesList == null) {
			System.out.println("空");
			this.sharedFilesList = new ArrayList<GetSharedBean>();
		} else {
			System.out.println("不为空");
		}
	}

	@Override
	public int getCount() {
		return sharedFilesList.size();
	}

	@Override
	public Object getItem(int position) {
		return sharedFilesList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(friendDetailActivity)
					.inflate(R.layout.item_friend_myshared_list, null, false);
			holder = new ViewHolder();

			GetSharedBean bean = sharedFilesList.get(position);

			holder.typeImg = (ImageView) convertView
					.findViewById(R.id.share_myshare_type_img);

			holder.typeImg.setImageDrawable(this.getFileIcon(
					bean.getFileName(), bean.isFile(), bean.isHomebox()));

			holder.downloadImg = (ImageView) convertView
					.findViewById(R.id.share_myshare_download_img);
			holder.downloadImg.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					friendDetailActivity.downloadMyShare(position);
				}
			});

			holder.cancelImg = (ImageView) convertView
					.findViewById(R.id.share_myshare_cancel_img);
			holder.cancelImg.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					friendDetailActivity.deleteMyShare(position);
				}

			});

			holder.cancelText = (TextView) convertView
					.findViewById(R.id.share_myshare_cancel_text);

			holder.cancelText.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					friendDetailActivity.deleteMyShare(position);
				}
			});
			holder.downloadText = (TextView) convertView
					.findViewById(R.id.share_myshare_download_text);
			holder.downloadText.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					friendDetailActivity.downloadMyShare(position);
				}

			});

			holder.fileInfo = (TextView) convertView
					.findViewById(R.id.share_myshare_fileInfo_text);
			if (bean.isFile()) {
				holder.fileInfo.setText(bean.getFileSize() + "	"
						+ bean.getShareDate());
			} else {
				holder.fileInfo.setText(bean.getShareDate());
			}

			holder.fileName = (TextView) convertView
					.findViewById(R.id.share_myshare_fileName_text);
			holder.fileName.setText(bean.getFileName());

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		return convertView;
	}

	private class ViewHolder {
		ImageView downloadImg, cancelImg,  typeImg;

		TextView fileName, fileInfo, downloadText, cancelText;
	}

	/**
	 * 
	 * @param fileName
	 * @param isFile
	 * @return
	 */
	private Drawable getFileIcon(String fileName, boolean isFile,
			boolean isHomeBox) {
		int imgId = 0;
		if (isFile) {
			String suffix = "";
			if (fileName.contains(".")) {
				suffix = fileName.substring(fileName.lastIndexOf("."))
						.toLowerCase(Locale.CHINA);
			}
			if (suffix.equals(".mp3") || suffix.equals(".acc")
					|| suffix.equals("APE") || suffix.equals("wav")) {
				imgId = R.drawable.files_icon_music;
			} else if (suffix.equals(".png") || suffix.equals(".jpg")
					|| suffix.equals(".bmp")) {
				imgId = R.drawable.files_icon_image;
			} else if (suffix.equals(".avi") || suffix.equals(".rmvb")
					|| suffix.equals(".mpg") || suffix.equals(".3gp")
					|| suffix.equals(".mp4") || suffix.equals(".rm")
					|| suffix.equals(".mkv") || suffix.equals(".flv")) {
				imgId = R.drawable.files_icon_movie;
			} else {
				imgId = R.drawable.files_icon_txt;
			}

		} else if (isHomeBox) {
			imgId = R.drawable.files_icon_homebox;
		} else {
			imgId = R.drawable.files_icon_folder;
		}

		return friendDetailActivity.getResources().getDrawable(imgId);
	}
	
	public void setData(List<GetSharedBean> myShareData) {
		sharedFilesList = myShareData;
		notifyDataSetInvalidated();
	}
}
